﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Webdiyer.WebControls.Mvc;

namespace MvcWeb.Controllers
{
    public class BrandSupplyController : BaseController
    {
        // GET: /BrandSupply/
        public ActionResult Index(int pageindex = 1, int pageSize = 20, int brandSupplyType = -1) //(int)MvcWeb.EAMEnum.SupplyBrand.供应商)
        {
            var iquery = db.AssetBrandSupply.AsQueryable();

            if (brandSupplyType != -1)
            {
                iquery = iquery.Where(m => m.BrandSupplyType == brandSupplyType);
            }
            iquery = iquery.OrderBy(m => m.BrandSupplyType);

            var list = iquery.ToPagedList(pageindex, pageSize);

            var dict = db.AssetCategory.ToList().ToDictionary(a => a.CategoryID, b => b.CategoryName);
            dict.Add(-1, "");
            ViewData["parentCategoryDic"] = dict;

            ViewData["BrandSupplyType"] = new SelectList(MvcWeb.EAMEnum.EAMEnumExt.ToListItem<MvcWeb.EAMEnum.SupplyBrand>(), "value", "text");

            ViewData["BrandSupplyTypeList"] = MvcWeb.EAMEnum.EAMEnumExt.ToDictory<MvcWeb.EAMEnum.SupplyBrand>();

            return View(list);
        }

        public ActionResult Edit(int id = -1)
        {
            EAM.Model.AssetBrandSupply model = null;
            if (id == -1)
            {
                model = new EAM.Model.AssetBrandSupply();
            }
            else
            {
                model = db.AssetBrandSupply.Find(id);
            }

            var dict = MvcWeb.EAMEnum.EAMEnumExt.ToDictory<MvcWeb.EAMEnum.SupplyBrand>();

            ViewData["BrandSupplyTypeList"] = dict;

            return View(model);
        }

        [HttpPost]
        public ActionResult Edit(int id, EAM.Model.AssetBrandSupply viewModel)
        {
            var user = System.Web.Security.Membership.GetUser();
            var guid = (Guid)user.ProviderUserKey;

            if (id == -1)
            {
                //添加关联信息
                EAM.Model.AssetBrandSupply model = new EAM.Model.AssetBrandSupply()
                {
                    BrandSupplyName = viewModel.BrandSupplyName,

                    BrandSupplyType = viewModel.BrandSupplyType,
                    BrandSupplyContactMember = viewModel.BrandSupplyContactMember,
                    BrandSupplyContactPhone = viewModel.BrandSupplyContactPhone,
                    BrandSupplyEmail = viewModel.BrandSupplyEmail,
                    BrandSupplyDescript = viewModel.BrandSupplyDescript,
                    BrandSupplyAddress = viewModel.BrandSupplyAddress,
                    BrandSupplyProdLicense = viewModel.BrandSupplyProdLicense,
                    BrandSupplyProdLicenseLastTime = viewModel.BrandSupplyProdLicenseLastTime,
                    BrandSupplyBussinessLicense = viewModel.BrandSupplyBussinessLicense,
                    BrandSupplyBussinessLicenseLastTime = viewModel.BrandSupplyBussinessLicenseLastTime,
                    LastUpdateMember = guid,
                    LastUpdateTime = DateTime.Now
                };
                db.AssetBrandSupply.Add(model);
            }
            else
            {
                var model = db.AssetBrandSupply.Find(id);
                db.AssetBrandSupply.Attach(model);

                model.BrandSupplyName = viewModel.BrandSupplyName;
                model.BrandSupplyType = viewModel.BrandSupplyType;
                model.BrandSupplyAddress = viewModel.BrandSupplyAddress;
                model.BrandSupplyContactMember = viewModel.BrandSupplyContactMember;
                model.BrandSupplyContactPhone = viewModel.BrandSupplyContactPhone;
                model.BrandSupplyDescript = viewModel.BrandSupplyDescript;
                model.BrandSupplyEmail = viewModel.BrandSupplyEmail;
                model.BrandSupplyProdLicense = viewModel.BrandSupplyProdLicense;
                model.BrandSupplyProdLicenseLastTime = viewModel.BrandSupplyProdLicenseLastTime;
                model.BrandSupplyBussinessLicense = viewModel.BrandSupplyBussinessLicense;
                model.BrandSupplyBussinessLicenseLastTime = viewModel.BrandSupplyBussinessLicenseLastTime;
                model.LastUpdateMember = guid;
                model.LastUpdateTime = DateTime.Now;
            }

            db.SaveChanges();
            return RedirectToAction("Index", new { typeid = viewModel.BrandSupplyType });
        }

        [HttpPost]
        public ActionResult Search(EAM.Model.AssetBrandSupply viewModel)
        {
            return Index();
        }

        public ActionResult DeleteChoose()
        {
            string ids = Request.Form["ids"];
            if (!string.IsNullOrWhiteSpace(ids))
            {
                string[] arr = ids.Split(',');
                foreach (var m in arr)
                {
                    var model = db.AssetBrandSupply.Find(int.Parse(m));
                    db.AssetBrandSupply.Remove(model);
                }
                db.SaveChanges();
            }
            return Json(true);
        }

        public ActionResult del(int id)
        {
            //资产中所有的删除都不允许
            //var model = db.AssetBrandSupply.Find(id);
            //db.AssetBrandSupply.Remove(model);
            //db.SaveChanges();
            return Json(true);
        }

    }
}
